package edu.univercidade.tcc.sap.client.widget;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

import edu.univercidade.tcc.sap.client.Messages;
import edu.univercidade.tcc.sap.client.UserService;
import edu.univercidade.tcc.sap.client.UserServiceAsync;
import edu.univercidade.tcc.sap.shared.FieldVerifier;

public class LoginDialog extends DialogBox {
	private final Button sendButton = new Button();
	private final TextBox nameField = new TextBox();
	private final PasswordTextBox passwordField = new PasswordTextBox();
	private final Label errorLabel = new Label();
	private final VerticalPanel vPanel = new VerticalPanel();
	private final Messages messages = GWT.create(Messages.class);
	private final UserServiceAsync userService = GWT.create(UserService.class);

	public LoginDialog() {
		sendButton.setText(messages.sendButton());
		nameField.setText(messages.nameField());
		passwordField.setText(messages.password());
		sendButton.addStyleName("sendButton");

		this.add(vPanel);
		vPanel.add(nameField);
		vPanel.add(passwordField);
		vPanel.add(sendButton);
		vPanel.add(errorLabel);

		LoginHandler handler = new LoginHandler();
		sendButton.addClickHandler(handler);
		nameField.addKeyUpHandler(handler);
		passwordField.addKeyUpHandler(handler);

		nameField.setFocus(true);
		nameField.selectAll();
	}

	private class LoginHandler implements ClickHandler, KeyUpHandler {
		/**
		 * Fired when the user clicks on the sendButton.
		 */
		public void onClick(ClickEvent event) {
			sendNameToServer();
		}

		/**
		 * Fired when the user types in the nameField.
		 */
		public void onKeyUp(KeyUpEvent event) {
			if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
				sendNameToServer();
			}
		}

		/**
		 * Send the name from the nameField to the server and wait for a
		 * response.
		 */
		private void sendNameToServer() {
			// First, we validate the input.
			errorLabel.setText("");
			String textToServer = nameField.getText();
			if (!FieldVerifier.isValidName(textToServer)) {
				errorLabel.setText("Please enter at least four characters");
				return;
			}

			// Then, we send the input to the server.
			sendButton.setEnabled(false);
			userService.isValidUser(nameField.getText(),
					passwordField.getText(), new AsyncCallback<Boolean>() {

						public void onFailure(Throwable arg0) {
							LoginDialog.this.setText(messages
									.failedLoginTitle());
							errorLabel.addStyleName("serverResponseLabelError");
							errorLabel.setText(messages.serverError());
							LoginDialog.this.center();
							sendButton.setEnabled(true);
							sendButton.setFocus(true);
						}

						public void onSuccess(Boolean validated) {
							if (validated) {
								LoginDialog.this.hide();
							} else {
								LoginDialog.this.setText(messages
										.failedLoginTitle());
								errorLabel
										.removeStyleName("serverResponseLabelError");
								errorLabel.setText(messages
										.failedLoginMessage());
								LoginDialog.this.center();
								sendButton.setEnabled(true);
								sendButton.setFocus(true);
							}
						}
					});
		}
	}
}
